package cm.mefobe.android.telecomcost.adapter;

import java.util.ArrayList;
import java.util.List;

import cm.mefobe.android.telecomcost.entities.Day;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class DayAdapter extends ArrayAdapter<Day> {
	private ArrayList<Day> items;
	private Context _context;	

	public DayAdapter(Context context, int textViewResourceId,
			ArrayList<Day> items) {
		super(context, textViewResourceId, items);
		this.items = items;
		_context = context;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public DayAdapter(Context context, int resource,
			int textViewResourceId, ArrayList<Day> objects) {
		super(context, resource, textViewResourceId, objects);
		this.items = objects;
		_context = context;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 */
	public DayAdapter(Context context, int resource, int textViewResourceId) {
		super(context, resource, textViewResourceId);
		_context = context;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public DayAdapter(Context context, int textViewResourceId,
			List<Day> objects) {
		super(context, textViewResourceId, objects);
		_context = context;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public DayAdapter(Context context, int textViewResourceId,
			Day[] objects) {
		super(context, textViewResourceId, objects);
		_context = context;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 */
	public DayAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		_context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i("DayAdapter", "getting Day view");
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(
					cm.mefobe.android.telecomcost.R.layout.check_view, null);
		}

		final Day Day = items.get(position);
		if (Day != null) {
			final CheckBox chk = (CheckBox) v
					.findViewById(cm.mefobe.android.telecomcost.R.id.checkBox1);
			if (chk != null) {
				chk.setText(Day.getName());
				chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					public void onCheckedChanged(CompoundButton arg0,
							boolean arg1) {
						selectDay(Day, arg1);
						chk.setChecked(Day.isSelected());
					}
				});
			}
		}
		return v;
	}

	protected void selectDay(Day Day, boolean arg1) {
		Day.setSelected(arg1);

		if (arg1) {
			Log.i("DayAdapter", "day " + Day.getName()
					+ " checked");
		} else {
			Log.i("DayAdapter", "day " + Day.getName()
					+ " unchecked");
		}
	}
}
